#!/bin/bash


#### 需要配置的变量  等号两边不要留空格,不要在windows下编辑
src_host=198.199.108.10
src_db_name=root
src_db_pwd=123456
src_db_schema=appbricks


dst_host=104.236.154.96
dst_user_name=core
dst_ssh_file="/home/wzq/office/digitalocean_104.236.154.96"
dst_file_path="/home/core/backup/mysql/mysql_appbricks_2016-06-21.sql.tgz"

local_docker_name=mysql_v3 # 如果本地没有mysqldump,可以从docker中获取
#### 需要配置的变量  等号两边不要留空格,不要在windows下编辑


local_file=local_file

#### step1. mysql must exist
if [ -z "$local_docker_name" ]; then
    mysql &> /dev/null
    if [ $? -eq 127 ]  ; then
        echo "mysql not exist"
        exit 1
    fi
fi


#### step2.  mysql

_scp="scp -i $dst_ssh_file $dst_user_name@$dst_host:$dst_file_path $local_file"
echo $_scp
eval $_scp

if [ -z "$local_docker_name" ]; then
    _cmd="tar -zOxvf $local_file | mysql -h $src_host -u $src_db_name -p$src_db_pwd $src_db_schema "
    echo $_cmd
    eval $_cmd
else
    echo "user docker container $local_docker_name "
    tar -zOxvf $local_file | cat > restore_sql
    docker cp restore_sql $local_docker_name:/tmp/restore_sql
    _cmd=" docker exec $local_docker_name sh -c 'mysql -h $src_host -u $src_db_name -p$src_db_pwd $src_db_schema < /tmp/restore_sql' "
    echo $_cmd
    eval $_cmd

    #clean up
    docker exec $local_docker_name sh -c 'rm /tmp/restore_sql'
    rm restore_sql
fi



rm $local_file
